package com.longer.playhelper.view.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.longer.playhelper.Application;
import com.longer.playhelper.R;
import com.longer.playhelper.modle.bean.User;
import com.longer.playhelper.utlis.Toast;
import com.longer.playhelper.view.fragment.LostFragment;
import com.longer.playhelper.view.fragment.MainFragment;
import com.longer.playhelper.view.fragment.PlayFragment;
import com.longer.playhelper.view.fragment.UserFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    public static AHBottomNavigation bottomNavigation;
    private long exitTime;//记录2次返回键的时间
    public static MainActivity instance = null;


    private UserFragment fg_user;
    private PlayFragment fg_play;
    private LostFragment fg_lost;
    private MainFragment fg_main;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        instance = MainActivity.this;
        ButterKnife.bind(this);

//        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        toolbar.setTitle("小玩帮");
        init();
        setbottom();
        showfragmenu();
    }

    public void setbottom() {
        bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);

        AHBottomNavigationItem item1 = new AHBottomNavigationItem("小玩帮", R.drawable.ic_home_black_24dp, R.color.colorPrimary);
        AHBottomNavigationItem item2 = new AHBottomNavigationItem("互动", R.drawable.ic_dashboard_black_24dp, R.color.colorPrimary);
        AHBottomNavigationItem item3 = new AHBottomNavigationItem("失物招领", R.drawable.ic_notifications_black_24dp, R.color.colorAccent);
        AHBottomNavigationItem item4 = new AHBottomNavigationItem("个人中心", R.drawable.ic_main_user, R.color.colorAccent);

// Add items
        bottomNavigation.addItem(item1);
        bottomNavigation.addItem(item2);
        bottomNavigation.addItem(item3);
        bottomNavigation.addItem(item4);
        bottomNavigation.setBehaviorTranslationEnabled(false);

        bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#fafbfe"));


        bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                hideFragment(ft);
                if (position == 0 && !wasSelected) {
                    toolbar.setTitle("小玩帮");
                    if (fg_main == null) {
                        fg_main = new MainFragment();
                        ft.add(R.id.content, fg_main);
                    }
                    ft.show(fg_main);
                    ft.commit();
                    return true;
                } else if (position == 1 && !wasSelected) {
                    toolbar.setTitle("互动");
                    if (fg_play == null) {
                        fg_play = new PlayFragment();
                        ft.add(R.id.content, fg_play);
                    }
                    ft.show(fg_play);
                    ft.commit();
                    return true;
                } else if (position == 2 && !wasSelected) {
                    toolbar.setTitle("失物招领");
                    if (fg_lost == null) {
                        fg_lost = new LostFragment();
                        ft.add(R.id.content, fg_lost);
                    }
                    ft.show(fg_lost);
                    ft.commit();
                    return true;
                } else if (position == 3 && !wasSelected) {
                    toolbar.setTitle("个人中心");
                    if (fg_user == null) {
                        fg_user = new UserFragment();
                        ft.add(R.id.content, fg_user);
                    }
                    ft.show(fg_user);
                    ft.commit();
                    return true;
                }
                return true;
            }
        });
    }


    /**
     * 方法，外面用来设置底部菜单所在位置
     *
     * @param num
     */
    public static void setBottomNavigation(int num) {
        bottomNavigation.setCurrentItem(num);
    }

    private void init() {
        User bmobUser = BmobUser.getCurrentUser(User.class);
        if (bmobUser != null) {
            // 允许用户使用应用
            Log.d("tip", "缓存账户不为空:" + bmobUser.getName());
            Application.setuser(bmobUser);
        }
    }

    /**
     * 默认加载主界面
     */
    private void showfragmenu() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideFragment(ft);
        if (fg_main == null) {
            fg_main = new MainFragment();
            ft.add(R.id.content, fg_main);
        }
        ft.show(fg_main);
        ft.commit();
    }

    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (fg_main != null) {
            transaction.hide(fg_main);
            fg_main = null;
        }
        if (fg_play != null) {
            transaction.hide(fg_play);
            fg_play = null;
        }
        if (fg_lost != null) {
            transaction.hide(fg_lost);
            fg_lost = null;
        }
        if (fg_user != null) {
            transaction.hide(fg_user);
            fg_user = null;
        }
    }

    /**
     * 点击返回按键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.show("再按一次退出");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
        }
        return true;
    }

    public void setTheme(boolean isnight) {
        int th = isnight ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
        getDelegate().setLocalNightMode(th);
        recreate();
    }
}
